package com.wlyuan.open.scanner.domain.address;

import com.wlyuan.open.scanner.constant.Constants;
import com.wlyuan.open.scanner.dao.entity.AddrDO;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lijing
 * @description
 */
public class AddressConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper
    public interface Convert {

        /**
         * 转换
         *
         * @param source
         * @return
         */
        @Mappings({
                @Mapping(target = "name", source = "addrSiteinfo"),
                @Mapping(target = "contactMobile", source = "addrPhone"),
                @Mapping(target = "customerId", source = "addrCustomerid"),
                @Mapping(target = "type", source = "addrType"),
                @Mapping(target = "supplierId", source = "addrSupplierid"),
                @Mapping(target = "symbolId", source = "addrSymbolid"),
                @Mapping(target = "id", source = "addrId"),

                @Mapping(target = "creatorAt", source = "addrCreator"),
                @Mapping(target = "createTime", source = "addrInserttime"),
                @Mapping(target = "updaterAt", source = "addrUpdater"),
                @Mapping(target = "updateTime", source = "addrUpdatetime"),
                @Mapping(target = "invalid", source = "addrInvalid"),
                @Mapping(target = "comments", source = "addrComments"),
                @Mapping(target = "location", source = "addrLocation"),
                @Mapping(target = "version", source = "addrVersion"),
                @Mapping(target = "locationType", source = "addrLocationtype"),
                @Mapping(target = "description", source = "addrDescription"),
                @Mapping(target = "status", source = "addrStatus"),
                @Mapping(target = "reorientation", source = "addrReorientation"),
                @Mapping(target = "errorUpdate", source = "addrErroupdate"),
        })
        @MappingCreation
        Address convert(AddrDO source);

    }

    public static List<Address> convert(List<AddrDO> sourceList) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Collections.emptyList();
        }
        return sourceList.stream().map(AddressConvert::convert).collect(Collectors.toList());
    }

    public static Address convert(AddrDO source) {
        var convert = CONVERT.convert(source);
        // 经纬度处理
        if (StringUtils.isNoneBlank(source.getAddrLocation()) && source.getAddrLocation().contains(Constants.LOCATION_SPLIT)) {
            String[] location = source.getAddrLocation().split(Constants.LOCATION_SPLIT);
            // 经度
            convert.setLon(location[0]);
            // 纬度 0-99
            convert.setLat(location[1]);
        }
        return convert;
    }

}
